package com.rem.cloudalibaba.config;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 自定义授权规则来源
 *
 * @author Rem
 * @date 2021/8/4
 */
@Component
@Log4j2
public class RequestOriginParserDefinition implements RequestOriginParser {

    @Override
    public String parseOrigin(HttpServletRequest request) {
        String serviceName = request.getParameter("serviceName");
        if (StringUtils.isNotBlank(serviceName)) {
            log.info("serviceName:{}", serviceName);
            return serviceName;
        }
        return request.getRemoteAddr();
    }

}
